//
//  APPTextController.m
//  i5db
//
//  Created by iMuse on 15-5-12.
//  Copyright (c) 2015年 mySelf. All rights reserved.
//

#import "APPTextController.h"
#import "AppCenter.h"
#import "NotificationNavView.h"

@interface APPTextController ()<UITextViewDelegate>
{
    UITextView * textView;
    UILabel    * line;
    NSString   * currentText;
    NSString   * tag;
    UILabel    * maxLabel;
    NSLayoutConstraint * textHeightConstraint;
    
}

@property (assign,nonatomic) NSInteger minNumber;
@property (assign,nonatomic) NSInteger maxNumber;

@end
@implementation APPTextController


-(id) initWith:(NSInteger) maxNumber minNumber :(NSInteger) minNumber tag :(NSString*) AppTag title :(NSString*) title text:(NSString *) _currentText;
{
    self = [super init];
    if (self)
    {
        self.minNumber = minNumber;
        self.maxNumber = maxNumber;
       
        tag        = AppTag;
        self.title          = title;
        currentText = _currentText;
    
        
    }
    
    return self;
}



-(void)initNavigationBar
{
    [super initNavigationBar];
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 44, 44);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    
   /* [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"edit.png"] forState:UIControlStateSelected];
    */
    
    [button addTarget:self action:@selector(saveText:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *temporaryBarButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    temporaryBarButtonItem2.style = UIBarButtonItemStylePlain;
    
    self.navigationItem.rightBarButtonItem = temporaryBarButtonItem2;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initNavigationBar];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    textView = [UITextView newAutoLayoutView];
    textView.delegate = self;
    textView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
  
    textView.font = [UIFont systemFontOfSize:15];
    textView.text = currentText;
    
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChanged:) name:UITextViewTextDidChangeNotification object:nil];

    
    if ([self.title isEqualToString:@"电话"] || [self.title isEqualToString:@"QQ"] || [self.title isEqualToString:@"人数上限"] )
    {
        textView.keyboardType = UIKeyboardTypeNumberPad;
    }
    
    if ([self.title isEqualToString:@"邮箱"])
    {
        textView.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
    [self.view addSubview:textView];
    
    maxLabel = [UILabel newAutoLayoutView];
    maxLabel.textColor = [AppCenter color:@"188,188,188,1"];
    maxLabel.text = [NSString stringWithFormat:@"0/%d",self.maxNumber];
    maxLabel.font = [UIFont systemFontOfSize:10];
    [self.view addSubview:maxLabel];

   
    [self performSelector:@selector(textResponder) withObject:nil afterDelay:0];
    
    [textView autoPinToTopLayoutGuideOfViewController:self withInset:0];
    [textView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [textView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];

    
  textHeightConstraint =   [textView autoSetDimension:ALDimensionHeight toSize:30];
    
    line = [UILabel newAutoLayoutView];
    [self.view addSubview:line];
    [line autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textView withOffset:0];
    [line autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
    [line autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    [line autoSetDimension:ALDimensionHeight toSize:1];
    line.backgroundColor = [AppCenter color:@"76,180,215"];
    
    
    [maxLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:textView withOffset:2];
    [maxLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
    
    
}

-(void)saveText:(UIButton*)button
{
   
    if (![self checkNumber])
    {
        
        return;
    }
    
    
    if ([self.delegate respondsToSelector:@selector(textViewString:text:)])
    {
        [self.delegate performSelector:@selector(textViewString:text:) withObject:tag withObject:textView.text];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) hiddent
{
    maxLabel.hidden = YES;
}

-(void)textDidChanged:(UITextView*)_textView
{
    [self textViewHeight];
    
        NSInteger  length = [textView.text length];
        maxLabel.text = [NSString stringWithFormat:@"%d/%d",length,self.maxNumber];
    
}


-(void) textViewHeight
{
    
    CGSize size =   [AppCenter sizeWithString:textView.text font:textView.font maxHeight:SCREEN_HEIGHT-70 maxWeight:SCREEN_WIDTH];
    
    if (size.height>30) {
        textHeightConstraint.constant = size.height+30;
    }
}

-(BOOL)checkNumber
{
    
    if ([textView.text length]>self.maxNumber)
    {
        [NotificationNavView showMessage:[NSString stringWithFormat:@"字数不能大于%d字",self.maxNumber]];
        
        return NO;
    }
    
    if ([textView.text length]<self.minNumber)
    {
        [NotificationNavView showMessage:[NSString stringWithFormat:@"字数不能小于%d个字",self.minNumber]];
       
        return  NO;
    }
    return YES;
}

-(void)textResponder
{
   [textView becomeFirstResponder];
}
- (void)viewWillAppear:(BOOL)animated {
    
    [self textDidChanged:nil];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:NO];
    [super viewWillAppear:animated];
    
    
}


- (void)viewWillDisappear:(BOOL)animated {
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:NO];
    
    [super viewWillDisappear:animated];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}



@end
